home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utmisc2 / countlns.lha / CountLines / CountLines.c < prev    next >
C/C++ Source or Header  |  1996-03-29  |  5KB  |  240 lines

  1. /*
  2. **  $VER: CountLines 1.0 (29 Mar 1996)  **
  3. **
  4. **        © 1996 Timo C. Nentwig
  5. **          all rights reserved !
  6. **
  7. **        tcn@techbase.in-berlin.de
  8. **
  9. ** ======================================
  10. **
  11. **  Language:
  12. **  ¯¯¯¯¯¯¯¯
  13. **
  14. **    Program is compiled by SAS/C
  15. **
  16. **
  17. **  Purpose:
  18. **  ¯¯¯¯¯¯¯
  19. **
  20. **    Returns the number of lines in
  21. **    specified file.
  22. **
  23. **    Scans the given file(s) and returns number of
  24. **        · lines
  25. **        · blank lines of lines
  26. **        · bytes of file
  27. **
  28. **    Pattern matching is supported.
  29. **    All arguments are worked up.
  30. **
  31. **
  32. **  Requirements:
  33. **  ¯¯¯¯¯¯¯¯¯¯¯¯
  34. **
  35. **    · AOS 2+
  36. **
  37. **
  38. **  Solve:
  39. **  ¯¯¯¯¯
  40. **
  41. **
  42. **
  43. **  Bugs:
  44. **  ¯¯¯¯
  45. **
  46. **
  47. **
  48. **  ToDo:
  49. **  ¯¯¯¯
  50. **
  51. **
  52. **  Notes:
  53. **  ¯¯¯¯¯
  54. **
  55. **
  56. **
  57. ** ======================================
  58. **
  59. **  History:
  60. **  ¯¯¯¯¯¯¯
  61. **
  62. **  29 Mar 1996 - 1.0 : initial release
  63. **
  64. */
  65.  
  66. /// include
  67.  
  68. #include <exec/types.h>
  69. #include <exec/memory.h>
  70.  
  71. #include <proto/exec.h>
  72. #include <proto/dos.h>
  73. #include <strings.h>
  74. #include <stdio.h>
  75.  
  76. ///
  77. /// protos
  78.  
  79. BOOL ScanFile (STRPTR path, LONG *lines, LONG *blanks, LONG *bytes);
  80.  
  81. ///
  82. /// main
  83.  
  84. VOID
  85. main (UWORD argc, STRPTR argv[])
  86. {
  87.  
  88.     if (argc == 1 || *argv[1] == '?')
  89.     {
  90.  
  91.         printf ("USAGE   : %s <path/file> or <path/pattern>\n", argv[0]);
  92.         printf ("EXAMPLES:\n");
  93.         printf ("\t  %s docs:#?.doc lists:stats/#?.lst <...>\n", argv[0]);
  94.         printf ("\t  %s text:ascii_file docs:*.doc <...>\n",     argv[0]);
  95.         printf ("\t  %s text:ascii_file <...>\n",                argv[0]);
  96.  
  97.     }
  98.     else
  99.     {
  100.  
  101.         #define    BUFFSIZE    240
  102.  
  103.         if (NULL != (DOSBase = (struct DosLibrary *) OpenLibrary ("dos.library", 37)))
  104.         {
  105.  
  106.             struct    AnchorPath   *ap;
  107.             LONG      err;
  108.  
  109.  
  110.             if (ap = AllocVec (sizeof (struct AnchorPath) + BUFFSIZE, MEMF_CLEAR))
  111.             {
  112.  
  113.                 UWORD    cnt;
  114.                 LONG     number    =  1;
  115.                 LONG     g_lines   =  0;
  116.                 LONG     g_blanks  =  0;
  117.                 LONG     g_bytes   =  0;
  118.  
  119.  
  120.                 printf ("Couting ...\n\n");
  121.  
  122.                 printf ("                             cnt  |  lines   |  blanks  |  bytes\n");
  123.                 printf ("                             -----+----------+----------+-----------\n");
  124.  
  125.                 for (cnt = 1; cnt < argc; cnt++)
  126.                 {
  127.  
  128.                     ap -> ap_Strlen = BUFFSIZE;
  129.  
  130.                     for (err = MatchFirst (argv[cnt], ap); err == 0; err = MatchNext (ap), number++)
  131.                     {
  132.  
  133.                         LONG    l_lines   =  0;
  134.                         LONG    l_blanks  =  0;
  135.                         LONG    l_bytes   =  0;
  136.  
  137.  
  138.                         if (ScanFile (ap -> ap_Buf, &l_lines, &l_blanks, &l_bytes) == TRUE)
  139.                         {
  140.  
  141.                             g_lines   +=  l_lines;
  142.                             g_blanks  +=  l_blanks;
  143.                             g_bytes   +=  l_bytes;
  144.  
  145.                             printf ("%25s: ", FilePart (ap -> ap_Buf));
  146.                             printf (" %4ld  |  %6ld  |  %6ld  |  %8ld\n", number, l_lines, l_blanks, l_bytes);
  147.  
  148.                         }
  149.                         else
  150.                         {
  151.  
  152.                             printf ("Couldn't access file \"%s\"\n", ap -> ap_Buf);
  153.  
  154.                         }
  155.  
  156.                         CloseLibrary ((struct Library *) DOSBase);
  157.  
  158.                     }
  159.  
  160.                 }
  161.                 
  162.  
  163.                 if (number > 2)
  164.                 {
  165.  
  166.                     printf ("==================================+==========+==========+==========\n");
  167.                     printf ("       Complete Statistic:           %6ld  |  %6ld  |  %8ld\n", g_lines, g_blanks, g_bytes);
  168.  
  169.                 }
  170.  
  171.                 MatchEnd (ap);
  172.                 FreeVec  (ap);
  173.  
  174.             }
  175.  
  176.         }
  177.  
  178.     }
  179.  
  180. }
  181.  
  182. ///
  183. /// ScanFile
  184.  
  185. BOOL
  186. ScanFile (STRPTR path, LONG *lines, LONG *blanks, LONG *bytes)
  187. {
  188.  
  189.              BPTR    file;
  190.     REGISTER LONG    i;
  191.     REGISTER UBYTE   line[1000];
  192.     REGISTER LONG    c;
  193.  
  194.  
  195.     if ((file = Open (path, MODE_OLDFILE)) != NULL)
  196.     {
  197.  
  198.         for (i = 0; (c = FGetC (file)) != EOF; i++)
  199.         {
  200.  
  201.             *bytes += 1;
  202.  
  203.             if (c == '\n')
  204.             {
  205.                 line[i] = '\0';
  206.                 i = -1;
  207.                 *lines += 1;
  208.  
  209.                 if (strlen (line) < 1)
  210.                    *blanks += 1;
  211.  
  212.             }
  213.             else
  214.             {
  215.  
  216.                if (i >= 999)
  217.                    i = -1;
  218.  
  219.                line[i] = c;
  220.  
  221.             }
  222.  
  223.         }
  224.  
  225.         Close (file);
  226.         return (TRUE);
  227.  
  228.     }
  229.     else
  230.     {
  231.  
  232.         return (FALSE);
  233.  
  234.     }
  235.  
  236. }
  237.  
  238. ///
  239.  
  240.